/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 23735
 * Date: 2023-09-22
 * Time: 23:46
 */
public class Solution {

    /**
     *  二分查找
     *  直接将二维数组变为一维数组
     */
    public boolean searchMatrix(int[][] matrix, int target) {
        int row = matrix.length;
        int col = matrix[0].length;
        int left = 0;
        int right = row * col - 1;
        while (left <= right) {
            int mid = ((right-left)>>1) + left;
            // 注意 1 维数组, 下标是从 0 开始的, 所以说: r = mid/col, c = mid%col
            if (matrix[mid/col][mid%col] < target) {
                left = mid + 1;
            } else if (matrix[mid/col][mid%col] > target) {
                right = mid - 1;
            } else {
                return true;
            }
        }
        return false;
    }
}
